package net.yebaihe.buildapp.yyh;

import java.util.HashMap;

import net.yebaihe.sdk.SdkUtils;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteDatabase.CursorFactory;

class MyHelper extends SQLiteOpenHelper
{
	public static final String DB_NAME = "buildapp";
	public static final int DB_VERSION = 1;
	private Context ctx;
	public MyHelper(Context context,String name,CursorFactory factory, int version)
	{
		super(context, name, factory, version);
		this.ctx=context;
	}
	@Override
	public void onCreate(SQLiteDatabase arg0) {
		//Attention:注意SQL语法，每个变量后需要有空格，否则不认识。
		/*
		 * Template表 id name sampleurl desc icon
		 * TemplateParams id tid tseq name type desc   ::::type 1:single file; 2:file list 3:String 4:version 5 checkbox :::: tid 0 all
		 * App表  id cname ename ttype
		 * AppParams id appid pid value
		 * 
		 */
		
		arg0.execSQL("CREATE TABLE IF NOT EXISTS template"
				+" (_id INTEGER PRIMARY KEY, name text,sampleurl text,desc text,icon text)");		
		arg0.execSQL("CREATE TABLE IF NOT EXISTS tparams"
				+" (_id INTEGER PRIMARY KEY, tid integer, tseq integer,name text, type text, desc text)");		
		arg0.execSQL("CREATE TABLE IF NOT EXISTS app"
				+" (_id INTEGER PRIMARY KEY, cname text,ename text, icon text, tid integer, versioncode integer)");		
		arg0.execSQL("CREATE TABLE IF NOT EXISTS aparams"
				+" (_id INTEGER PRIMARY KEY, aid integer, tid integer, value text)");		
	}
	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		//db.execSQL("DROP TABLE IF EXISTS "+TB_NAME);
		//onCreate(db);
	}
	public static MyHelper get(Context ctx) {
		return new MyHelper(ctx,MyHelper.DB_NAME,null,MyHelper.DB_VERSION);
	}
	public HashMap<Integer, String> getAppValueMap(int appidx) {
		HashMap<Integer,String>valuemap=new HashMap<Integer,String>();

		SQLiteDatabase db = getReadableDatabase();
		
		Cursor cursor=db.rawQuery("select * from aparams where aid="+appidx, null);
		if (cursor.moveToFirst()){
			do{
				valuemap.put(cursor.getInt(2),cursor.getString(3));
			}while (cursor.moveToNext());
		}
		cursor.close();

		cursor=db.rawQuery("select * from app where _id="+appidx, null);
		if (cursor.moveToFirst()){
			valuemap.put(-1,cursor.getString(5));
			valuemap.put(-2,cursor.getString(1));
			valuemap.put(-3,cursor.getString(2));
			valuemap.put(-4,cursor.getString(3));
			valuemap.put(-5,cursor.getString(4));
			valuemap.put(-6,SdkUtils.getIMEI(ctx));
			valuemap.put(-7,ctx.getPackageName());
		}
		cursor.close();
		db.close();    
		close();		
		return valuemap;
	}
}	

